﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class AccountPnController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private AccountPnService accountPnService;
	
	private AccountPn accountPn;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setAccountPnServiceImpl(AccountPnService service) {
		this.accountPnService = service;
	}	
	
	
	
	public void setOfferId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	public void get(HttpServletRequest request,HttpServletResponse response,AccountPn accountPn) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			AccountPn oldAccountPn = (AccountPn)accountPnService.getById(accountPn.getId());
                         
			
            //取招聘岗位
			
			if(oldAccountPn!=null) result.put("data", oldAccountPn);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}


	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,AccountPnQuery accountPnQuery) throws IOException
	{
		
		
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		ListRange<AccountPn> resultList=new ListRange();
		
		try{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			accountPnQuery.setOnlineUser(onlineUser);
		}	
		Page page = accountPnService.findPage(accountPnQuery);
		
		List<AccountPn> AccountPnlist = (List) page.getResult();
		resultList = new ListRange<AccountPn>();
		resultList.setRows(AccountPnlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");		
		resultList.setSuccess(true);
		}
		catch (Exception e)
		{
			resultList.setMessage("系统异常");		
			resultList.setSuccess(false);
			e.printStackTrace();
		}
				
		outJson(response,resultList);
		
		
	}
	public String managerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/accountPnManagerPage.jsp";	
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,AccountPn accountPn) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(AccountPn.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(accountPn,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(AccountPn.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(accountPn,"createDt",new Date());}catch(Exception ex){}
				}

			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			
		       try{
		    	   multipartRequest = (MultipartHttpServletRequest) request;
		    	   multipartFile = multipartRequest.getFile("uploadFile");
		       }catch(Exception ex){ex.printStackTrace();}

		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){
		    	   accountPnService.saveFile(multipartRequest, multipartFile, accountPn);		    	   
		       }
		       
			accountPnService.save(accountPn);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,AccountPn accountPn) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        AccountPn oldaccountPn = (AccountPn)accountPnService.getById(accountPn.getId());
                         
			bind(request,oldaccountPn);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(AccountPn.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldaccountPn,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(AccountPn.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldaccountPn,"modifiedDt",new Date());}catch(Exception ex){}
				}

			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			
		       try{
		    	   multipartRequest = (MultipartHttpServletRequest) request;
		    	   multipartFile = multipartRequest.getFile("uploadFile");
		       }catch(Exception ex){ex.printStackTrace();}

		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){
		    	   accountPnService.saveFile(multipartRequest, multipartFile, oldaccountPn);		    	   
		       }   
			accountPnService.update(oldaccountPn);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void autogen(HttpServletRequest request,HttpServletResponse response,AccountPn accountPn) throws IOException
	{
		//
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			 AccountPn oldaccountPn = (AccountPn)accountPnService.getById(accountPn.getId());
			 bind(request,oldaccountPn);
             //设置修改用户和修改时间 
			 com.hh.bss.auth.client.OnlineUser onlineUser=null;
             com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					 onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(AccountPn.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldaccountPn,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(AccountPn.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldaccountPn,"modifiedDt",new Date());}catch(Exception ex){}
				}
			//
			accountPnService.genPnFile(request,onlineUser, oldaccountPn);
			accountPnService.update(oldaccountPn);
			result.put("success", true);
			result.put("file", oldaccountPn.getUploadDoc());
			result.put("msg", "生成成功!");
			
			
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
		
		
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				accountPnService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
